﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKArchUtils.Utils;
using YJKRevitDimensionText.OneKeyPlanDim.Utility;

namespace YJKRevitDimensionText.OneKeyPlanDim.LevelDim
{
	public class ParapetLevelDim
	{
		public ParapetLevelDim(Autodesk.Revit.DB.Document doc, List<Wall> lstDaughterWall)
		{
			this.m_doc = doc;
			this.m_lstDaughterWall = lstDaughterWall;
		}

		public void CreateLevelDim()
		{
			this.ClassificationDaughterWall();
			LevelDimData levelDimData = new LevelDimData(this.m_doc);
			foreach (CommonParapet commonParapet in this.m_lstCommonParapet)
			{
				double num = double.MinValue;
				foreach (Wall wall in commonParapet.m_lstDaughterWall)
				{
					double num2 = RevitElementExtension.GetParameter(wall, BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble();
					if (Geometry.LessThan(num, num2))
					{
						num = num2;
					}
				}
				List<Wall> list = new List<Wall>();
				foreach (Wall wall2 in commonParapet.m_lstDaughterWall)
				{
					double num3 = RevitElementExtension.GetParameter(wall2, BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble();
					if (Geometry.IsEqual(num, num3))
					{
						list.Add(wall2);
					}
				}
				if (list.Count > 0)
				{
					Curve curve = (list[0].Location as LocationCurve).Curve;
					levelDimData.PtDimLocation = (curve.GetEndPoint(0) + curve.GetEndPoint(1)) / 2.0;
					levelDimData.DimVal = (Common.FeetToMM(commonParapet.m_dLevelVal) / 1000.0).ToString();
					commonParapet.CreateDim(levelDimData);
				}
			}
		}

		private void ClassificationDaughterWall()
		{
			foreach (Wall wall in this.m_lstDaughterWall)
			{
				CommonParapet commonParapet = new CommonParapet(this.m_doc);
				commonParapet.AnalysisWall(wall);
				bool flag = false;
				foreach (CommonParapet commonParapet2 in this.m_lstCommonParapet)
				{
					if (commonParapet.IsCommonWall(commonParapet2))
					{
						flag = true;
						commonParapet2.m_lstDaughterWall.Add(wall);
						break;
					}
				}
				if (!flag)
				{
					commonParapet.m_lstDaughterWall.Add(wall);
					this.m_lstCommonParapet.Add(commonParapet);
				}
			}
		}

		private Document m_doc;

		private List<Wall> m_lstDaughterWall = new List<Wall>();

		private List<CommonParapet> m_lstCommonParapet = new List<CommonParapet>();
	}
}
